// yb_mingpian/pages/cate_goods_list/index.js
var app = getApp(),
    core = app.requirejs("core");

Page({
  /**
   * 页面的初始数据
   */
  data: {
      cate_id: 0,
      goodList: [],
      page: 1,
      loaded: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var cate_id = options.cate_id;

      this.setData({
          cate_id: cate_id
      })

      this.getCateGoodsList();
  },

  // 获取商品分类商品列表
  getCateGoodsList: function () {
    var that = this,
        params = {cate_id: that.data.cate_id, page: that.data.page};
    console.log("param",params)
    core.get("goods/GoodsList", params, function (res) {
      console.log(res)
      if (res.code == 0) {
          if (res.info.length == 0){
              that.setData({
                  loaded: false
              });
              return false;
          }

          var new_data = {};
          new_data.page = that.data.page + 1;
          new_data.goodList = that.data.goodList.concat(res.info);

          if (res.info.length < 10) {
              new_data.loaded = false;
          }
              console.log("new_data_goodList",new_data.goodList)
          that.setData(new_data);
      } else {
          core.alert(res.msg)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.setData({
          goodList: [],
          page: 1,
          loaded: true
      })

      this.getCateGoodsList()
      wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if (this.data.loaded) {
          this.getCateGoodsList()
      }
  },

})